#include "../gui/WinEventFilter.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif

bool WinEventFilter::nativeEventFilter(const QByteArray &,
                                       void *msg,
                                       qintptr *) {
#ifdef Q_OS_WIN
    auto *m = static_cast<MSG *>(msg);
    if (m->message == WM_INPUT) {
        UINT sz = 0;
        GetRawInputData(reinterpret_cast<HRAWINPUT>(m->lParam),
                        RID_INPUT, nullptr, &sz,
                        sizeof(RAWINPUTHEADER));
        QByteArray buf(sz, Qt::Uninitialized);
        GetRawInputData(reinterpret_cast<HRAWINPUT>(m->lParam),
                        RID_INPUT, buf.data(), &sz,
                        sizeof(RAWINPUTHEADER));

        auto *ri = reinterpret_cast<RAWINPUT *>(buf.data());
        if (ri->data.mouse.usFlags == MOUSE_MOVE_RELATIVE) {
            emit rawMouseMove(ri->data.mouse.lLastX,
                              ri->data.mouse.lLastY);
        }
    }
#endif
    return false;
}
